<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_matakuliah extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'kd_matakuliah';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';  
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';  
		$kd_kurikulum = isset($_POST['kd_kurikulum']) ? strval($_POST['kd_kurikulum']) : '';  
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.id_matakuliah, c.kd_fakultas, c.nama_fakultas, b.kd_prodi, b.nama_prodi, a.kd_kurikulum, d.nama_kurikulum, a.kd_matakuliah, a.nama_matakuliah, a.sks, a.semester, a.status, a.ditawarkan, a.kd_jenismk, e.Nama as NamaJenisMK, a.kd_kelompokmk, f.Nama as NamaKelompokMK, a.mk_prasyarat, a.ditawarkan');
			$this->db->from('ref_matakuliah a');
			$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
			$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');
			$this->db->join('ref_kurikulum d', 'd.kd_kurikulum = a.kd_kurikulum');
			$this->db->join('ref_mkjenis e', 'e.kd_jenismk = a.kd_jenismk');
			$this->db->join('ref_mkkelompok f', 'f.kd_kelompokmk = a.kd_kelompokmk','left');
			
			$this->db->where('c.kd_fakultas', $kd_fakultas);
			$this->db->where('a.kd_prodi', $kd_prodi);
			$this->db->where('a.kd_kurikulum', $kd_kurikulum);
			$this->db->where('a.status', "A");
			
			$this->db->order_by('a.semester', 'ASC');
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('c.kd_fakultas, c.nama_fakultas, b.kd_prodi, b.nama_prodi, a.kd_kurikulum, d.nama_kurikulum, a.kd_matakuliah, a.nama_matakuliah, a.sks, a.semester, a.status, a.ditawarkan, a.kd_jenismk, a.kd_kelompokmk, f.Nama as NamaKelompokMK, a.mk_prasyarat, a.id_matakuliah');
		$this->db->from('ref_matakuliah a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');
		$this->db->join('ref_kurikulum d', 'd.kd_kurikulum = a.kd_kurikulum');
		$this->db->join('ref_mkjenis e', 'e.kd_jenismk = a.kd_jenismk','left');
		$this->db->join('ref_mkkelompok f', 'f.kd_kelompokmk = a.kd_kelompokmk','left');
		$this->db->where('a.id_matakuliah', $kode);
		
		return $this->db->get();
	}
	
	function togrid($data, $count){
		$response->total = $count;
		$response->rows = array();
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		return json_encode($response);
	}
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('id_matakuliah',$kode);
		
		$this->db->select('*');
		$this->db->from('ref_matakuliah');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kd_prodi', $data['kd_prodi']);
		$this->db->set('kd_kurikulum', $data['kd_kurikulum']);
		$this->db->set('kd_jenismk', $data['kd_jenismk']);
		$this->db->set('kd_kelompokmk', $data['kd_kelompokmk']);
		$this->db->set('kd_matakuliah', $data['kd_matakuliah']);
		$this->db->set('nama_matakuliah', $data['nama_matakuliah']);
		$this->db->set('sks', $data['sks']);
		$this->db->set('semester', $data['semester']);
		$this->db->set('mk_prasyarat', isset($data['mk_prasyarat'])?$data['mk_prasyarat']:'');
		$this->db->set('ditawarkan', $data['ditawarkan']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('CUID', $this->session->userdata('user_id'));
        $this->db->set('CDATE', date('Y-m-d H:i:s'));
		
		$result = $this->db->insert('ref_matakuliah');
		
		//pembobotan add
		$this->db->flush_cache();
		$this->db->set('kd_matakuliah', $data['kd_matakuliah']);
		$result = $this->db->insert('tbl_pembobotan');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		// update data
		$this->db->flush_cache();
		$this->db->set('kd_prodi', $data['kd_prodi']);
		$this->db->set('kd_kurikulum', $data['kd_kurikulum']);
		$this->db->set('kd_jenismk', $data['kd_jenismk']);
		$this->db->set('kd_kelompokmk', $data['kd_kelompokmk']);
		$this->db->set('kd_matakuliah', $data['kd_matakuliah']);
		$this->db->set('nama_matakuliah', $data['nama_matakuliah']);
		$this->db->set('sks', $data['sks']);
		$this->db->set('semester', $data['semester']);
		$this->db->set('mk_prasyarat', $data['mk_prasyarat']);
		$this->db->set('ditawarkan', $data['ditawarkan']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('id_matakuliah', $data['kode']);
		$result = $this->db->update('ref_matakuliah');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($kode){
		//query delet data  dengan ganti status db            
		$this->db->flush_cache();
		
		$this->db->set('status', "T");
	   
		$this->db->set('MUID', $this->session->userdata('user_id'));
		$this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('id_matakuliah', $kode);
		$result = $this->db->update('ref_matakuliah');
	   
	   
		//return
		if($result) {
				return TRUE;
		}else {
				return FALSE;
		}
	}
	
	// ------------ SAP
	function getIntro($kd_prodi, $kd_matakuliah){
		$this->db->where('kd_prodi', $kd_prodi);
		$this->db->where('kd_matakuliah', $kd_matakuliah);
		$this->db->from('ref_mksapintro');
		
		return $this->db->get();
	}
	
	function getSap($kd_prodi, $kd_matakuliah){
		$this->db->where('kd_prodi', $kd_prodi);
		$this->db->where('kd_matakuliah', $kd_matakuliah);
		$this->db->from('ref_mksap');
		
		return $this->db->get();
	}
	
	function IntroExist($kd_prodi, $kd_matakuliah){
		$this->db->where('kd_prodi', $kd_prodi);
		$this->db->where('kd_matakuliah', $kd_matakuliah);
		$this->db->from('ref_mksapintro');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function SapExist($kd_prodi, $kd_matakuliah, $kd_materi){
		$this->db->where('kd_prodi', $kd_prodi);
		$this->db->where('kd_matakuliah', $kd_matakuliah);
		$this->db->where('kd_materi', $kd_materi);
		$this->db->from('ref_mksap');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function InsertSAP($data){
		$this->db->trans_start();
		
		$result = true;
		
		# tambah intro ke table
		$this->db->flush_cache();
        $this->db->set('kd_pt', $this->mdl_prosedur->getKodePT()); //inject
        $this->db->set('kd_prodi', $data['kd_prodi']);
        $this->db->set('kd_matakuliah', $data['kd_matakuliah']);
        $this->db->set('deskripsi', $data['deskripsi']);
        $this->db->set('kompetensi', $data['kompetensi']);
        $this->db->set('daftar_pustaka', $data['daftar_pustaka']);
        $this->db->set('kompetensi_dosen', $data['kompetensi_dosen']);
        $this->db->set('status', 'A');
		
		if($this->IntroExist($data['kd_prodi'], $data['kd_matakuliah'])){
		
			$this->db->set('MUID', $this->session->userdata('user_id'));
			$this->db->set('MDATE', date('Y-m-d H:i:s'));
			
			$this->db->where('kd_prodi', $data['kd_prodi']);
			$this->db->where('kd_matakuliah', $data['kd_matakuliah']);
			
			$result = $this->db->update('ref_mksapintro');
			
		}else{
			$this->db->set('CUID', $this->session->userdata('user_id'));
			$this->db->set('CDATE', date('Y-m-d H:i:s'));
			
			$result = $this->db->insert('ref_mksapintro');
		}
		
		# tambah sap ke tabel
		foreach($data['data_sap']['rows'] as $row){
			if($row['kd_materi'] != '' && $row['Materi'] != ''){
				
				$this->db->flush_cache();
				$this->db->set('kd_pt', $this->mdl_prosedur->getKodePT());
				$this->db->set('kd_prodi', $data['kd_prodi']);
				$this->db->set('kd_matakuliah', $data['kd_matakuliah']);
				$this->db->set('kd_materi', $row['kd_materi']);
				$this->db->set('Materi', $row['Materi']);
				$this->db->set('Waktu', $row['Waktu']);
				$this->db->set('Metode', $row['Metode']);
				
				$this->db->set('status', 'A');
				
				if($this->SapExist($data['kd_prodi'], $data['kd_matakuliah'], $row['kd_materi'])){
					$this->db->set('MUID', $this->session->userdata('user_id'));
					$this->db->set('MDATE', date('Y-m-d H:i:s'));
					
					$this->db->where('kd_prodi', $data['kd_prodi']);
					$this->db->where('kd_matakuliah', $data['kd_matakuliah']);
					$this->db->where('kd_materi', $row['kd_materi']);
					
					$result = $this->db->update('ref_mksap');
				}else{
					$this->db->set('CUID', $this->session->userdata('user_id'));
					$this->db->set('CDATE', date('Y-m-d H:i:s'));
					$result = $this->db->insert('ref_mksap');
				}
			}
		}
		
		//return
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}
	
	function DeleteSAP($kd_prodi, $kd_matakuliah, $kd_materi){
		$this->db->flush_cache();
		$this->db->where('kd_prodi', $kd_prodi);
		$this->db->where('kd_matakuliah', $kd_matakuliah);
		$this->db->where('kd_materi', $kd_materi);
		return $this->db->delete('ref_mksap');
	}
	
	
	function get_pdf($fil){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
		$this->db->select('c.kd_fakultas, c.nama_fakultas, b.kd_prodi, b.nama_prodi, a.kd_kurikulum, d.nama_kurikulum, a.kd_matakuliah, a.nama_matakuliah, a.sks, a.semester, a.status, a.ditawarkan, a.kd_jenismk, e.Nama as NamaJenisMK, a.kd_kelompokmk, f.Nama as NamaKelompokMK');
		$this->db->from('ref_matakuliah a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');
		$this->db->join('ref_kurikulum d', 'd.kd_kurikulum = a.kd_kurikulum');
		$this->db->join('ref_mkjenis e', 'e.kd_jenismk = a.kd_jenismk');
		$this->db->join('ref_mkkelompok f', 'f.kd_kelompokmk = a.kd_kelompokmk','left');
					
			$this->db->where('a.kd_prodi', $fil['kd_prodi']);
			$this->db->where('a.kd_kurikulum', $fil['kd_kurikulum']);
			
			$this->db->where('a.status', 'A');

		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
	}
	
	public function importData($data){
		//query insert data
		$this->db->flush_cache();
		
		$this->db->set('id_matakuliah',				$data['id_matakuliah']);
		$this->db->set('kd_kurikulum',				$data['kd_kurikulum']);
		$this->db->set('kd_prodi',					$data['kd_prodi']);
		$this->db->set('kd_matakuliah',				$data['kd_matakuliah']);
		$this->db->set('nama_matakuliah',			$data['nama_matakuliah']);
		$this->db->set('sks',						$data['sks']);
		$this->db->set('semester',					$data['semester']);
		$this->db->set('kd_jenismk',				$data['kd_jenismk']);
		$this->db->set('kd_kelompokmk',				$data['kd_kelompokmk']);
		$this->db->set('mk_prasyarat',				$data['mk_prasyarat']);
		$this->db->set('ditawarkan',				$data['ditawarkan']);
		$this->db->set('status',					$data['status']);
		
		$result = $this->db->insert('ref_matakuliah');
		
		$errNo   = $this->db->_error_number();
	    $errMess = $this->db->_error_message();
		$error = $errMess;
		//var_dump($errMess);die;
	    log_message("error", "Problem import Inserting to : ".$errMess." (".$errNo.")"); 
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
}

?>